home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch10 / BinTree.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-08  |  3KB  |  93 lines

  1. VERSION 5.00
  2. Begin VB.Form frmBinTree 
  3.    Caption         =   "BinTree"
  4.    ClientHeight    =   4335
  5.    ClientLeft      =   2445
  6.    ClientTop       =   1335
  7.    ClientWidth     =   7470
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   4335
  11.    ScaleWidth      =   7470
  12.    Begin VB.TextBox txtDepth 
  13.       Height          =   285
  14.       Left            =   720
  15.       MaxLength       =   3
  16.       TabIndex        =   2
  17.       Text            =   "5"
  18.       Top             =   120
  19.       Width           =   615
  20.    End
  21.    Begin VB.CommandButton cmdGo 
  22.       Caption         =   "Go"
  23.       Default         =   -1  'True
  24.       Height          =   375
  25.       Left            =   360
  26.       TabIndex        =   1
  27.       Top             =   720
  28.       Width           =   615
  29.    End
  30.    Begin VB.PictureBox picCanvas 
  31.       AutoRedraw      =   -1  'True
  32.       Height          =   3495
  33.       Left            =   1440
  34.       ScaleHeight     =   229
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   293
  37.       TabIndex        =   0
  38.       Top             =   0
  39.       Width           =   4455
  40.    End
  41.    Begin VB.Label Label1 
  42.       Caption         =   "Depth"
  43.       Height          =   255
  44.       Left            =   120
  45.       TabIndex        =   3
  46.       Top             =   120
  47.       Width           =   495
  48.    End
  49. Attribute VB_Name = "frmBinTree"
  50. Attribute VB_GlobalNameSpace = False
  51. Attribute VB_Creatable = False
  52. Attribute VB_PredeclaredId = True
  53. Attribute VB_Exposed = False
  54. Option Explicit
  55. Private Const PI = 3.14159
  56. Private Const LENGTH_SCALE = 0.75
  57. Private Const DTHETA = PI / 5
  58. ' Recursively draw a binary tree branch.
  59. Private Sub DrawBranch(ByVal depth As Integer, ByVal X As Single, ByVal Y As Single, ByVal length As Single, ByVal theta As Single)
  60. Dim x1 As Integer
  61. Dim y1 As Integer
  62.     ' See where this branch should end.
  63.     x1 = X + length * Cos(theta)
  64.     y1 = Y + length * Sin(theta)
  65.     picCanvas.Line (X, Y)-(x1, y1)
  66.     ' If depth > 1, draw the attached branches.
  67.     If depth > 1 Then
  68.         DrawBranch depth - 1, x1, y1, length * LENGTH_SCALE, theta + DTHETA
  69.         DrawBranch depth - 1, x1, y1, length * LENGTH_SCALE, theta - DTHETA
  70.     End If
  71. End Sub
  72. Private Sub cmdGo_Click()
  73. Dim depth As Integer
  74. Dim start_length As Single
  75.     picCanvas.Cls
  76.     MousePointer = vbHourglass
  77.     DoEvents
  78.     If Not IsNumeric(txtDepth.Text) Then txtDepth.Text = "5"
  79.     depth = CInt(txtDepth.Text)
  80.     start_length = (picCanvas.ScaleHeight - 10) / _
  81.         ((1 - LENGTH_SCALE ^ (depth + 1)) / (1 - LENGTH_SCALE))
  82.     DrawBranch depth, picCanvas.ScaleWidth \ 2, _
  83.         picCanvas.ScaleHeight - 5, _
  84.         start_length, -PI / 2
  85.     MousePointer = vbDefault
  86. End Sub
  87. Private Sub Form_Resize()
  88. Dim wid As Single
  89.     wid = ScaleWidth - picCanvas.Left
  90.     If wid < 120 Then wid = 120
  91.     picCanvas.Move picCanvas.Left, 0, wid, ScaleHeight
  92. End Sub
  93.